package com.gmail.skwong.speedplayer;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;

import android.os.Environment;

/**
 * This class is responsible for reading all the files from the SD card and filtering them
 * for only mp3 files.
 */
public class SongsImporter {
	
	final String MEDIA_PATH = new String(Environment.getExternalStorageDirectory().getPath()); //Path to SD card
	private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
	
	//Default Constructor
	public SongsImporter() {
	}
	
	public ArrayList<HashMap<String, String>> getSongsList() {
		File home = new File(MEDIA_PATH);
		
		if (home.listFiles(new FileExtensionFilter()).length > 0) {
			for (File file : home.listFiles(new FileExtensionFilter())) {
				HashMap<String, String> song = new HashMap<String, String>();
				//get name of song but cutting extension
				song.put("songTitle", file.getName().substring(0, file.getName().length() -4));
				song.put("songPath", file.getPath());
				
				songsList.add(song);
			}
		}
		return songsList;
	}
	
	class FileExtensionFilter implements FilenameFilter {
		public boolean accept(File dir, String name) {
			return (name.endsWith(".mp3") || name.endsWith(".MP3") || name.endsWith(".Mp3") || name.endsWith(".mP3"));
		}
	}
}
